package com.cn.transfer.handle.impl.purchasearrival;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.*;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.vo.MessagePushVO;
import com.cn.transfer.handle.MessageStrategy;
import com.cn.transfer.mapper.VvPuPurchaseArrivalBMapper;
import com.cn.transfer.mapper.VvPuPurchaseArrivalMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2023/10/25 15:40
 * @Description: 进货单审核
 */
@Slf4j
@Component
public class PurchaseArrivalReviewImpl implements MessageStrategy {
    @Autowired
    private VvPuPurchaseArrivalMapper vvPuPurchaseArrivalMapper;
    @Autowired
    private VvPuPurchaseArrivalBMapper vvPuPurchaseArrivalBMapper;

    @Override
    public void handle(MessagePushVO message) {
        DocumentApproval documentApproval = ConnmeUtils.mapToObject(message.getBizContent(), DocumentApproval.class);
        VvPuPurchaseArrival vvPuPurchaseArrival = vvPuPurchaseArrivalMapper.byCode(documentApproval.getVoucherCode());
        // 查询视图，这条数据是否存在
        int i1 = vvPuPurchaseArrivalMapper.inquireIsExist(vvPuPurchaseArrival.getCode());
        if (i1 == 0) {
            // 存入视图
            vvPuPurchaseArrivalMapper.insert(vvPuPurchaseArrival);
            for (VvPuPurchaseArrivalB vvPuPurchaseArrivalB : vvPuPurchaseArrival.getArrival_list()) {
                vvPuPurchaseArrivalBMapper.insert(vvPuPurchaseArrivalB);
            }
        }
        Header[] originalHeaders = ConnmeUtils.createHeaders();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", "64f00ef131adb70007ad6e6d");
        parameter.put("entry_id", "6535dbd4c1285112c61d4ca0");
        parameter.put("data", ConnmeUtils.convertEntityToMap(vvPuPurchaseArrival));
        String jsonSale = JSONUtil.toJsonStr(parameter);
        log.info("采购进货单审核时发生的数据：==== " + jsonSale);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/create", originalHeaders, jsonSale);
        log.info("采购进货单审核时接收的数据：====" + jdySale);
    }

    @Override
    public String getMsgType() {
        return "PuArrival_Audit";
    }
}
